package org.smile.homework01.arthas;

public class Engine {
    private int id;
    private String name;
    private int workTime;
    private int coldDown;

    private int wk;
    private int cd;
    private boolean standby;

    public Engine(int id) {
        this.id = id;
        this.name = id + "号引擎";
        this.workTime = this.randomTime();
        this.coldDown = this.randomTime();

        this.wk = 0;
        this.cd = this.coldDown;
        this.standby = true;
    }

    public int randomTime() {
        return (int) (Math.random() * 10 + 1);
    }

    public void working() {
        if (this.wk < this.workTime) {
            this.wk++;
            System.out.println(this.name + "工作" + this.wk + "秒");

            if (this.wk == this.workTime) {
                this.standby = false;
                this.cd = this.coldDown;
                this.wk = 0;
                System.out.println(this.name + "进入冷却。");
            }
        } else {
            System.out.println(this.name + "工作异常。");
        }
    }

    public void colding() {
        if (this.cd > 0) {
            System.out.println(this.name + "冷却倒计时：" + this.cd + "秒");
            this.cd--;
        } else if (this.cd == 0) {
            this.standby = true;
            System.out.println(this.name + "结束冷却。");
        } else {
            System.out.println(this.name + "冷却异常。");
        }
    }

    public boolean isAvailable(int workingEngine) {
        if (this.id == workingEngine && this.standby) {
            return true;
        } else {
            return false;
        }
    }

    public int next() {
        if (standby) {
            return this.id;
        } else {
            return this.id + 1;
        }
    }

    public void prepare() {
        if (!standby) {
            this.colding();
        }
    }

    public void describe() {
        System.out.println(this.name + "工作时间：" + this.workTime + "秒,冷却时间：" + this.coldDown + "秒。");
    }
}
